package com.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import com.util.ConcurrentHashSet;

/**
 * 
 * @author zhoufang
 *
 * 2017年12月24日下午6:14:24
 */
public class ServerNettyManager {
	
	private static ConcurrentHashMap<Integer,ServerNettyConnect> serverConnects = new ConcurrentHashMap<Integer,ServerNettyConnect>();
	private static ConcurrentHashSet<Channel> unRegisterChannels = new ConcurrentHashSet<Channel>();
	
	private static EventLoopGroup bossGroup;
	private static EventLoopGroup workGroup;
	private static ServerBootstrap bootstrap;
	private static AtomicBoolean isStart = new AtomicBoolean(false);
	
	public static void start(int bossNumber, int workNumber,int port){
		if(isStart.get()){
			return;
		}
		isStart.set(true);
		bossGroup = new NioEventLoopGroup(bossNumber);
		workGroup = new NioEventLoopGroup(workNumber);
		bootstrap = new ServerBootstrap();
		bootstrap.group(bossGroup, workGroup);
		bootstrap.channel(NioServerSocketChannel.class);
		bootstrap.handler(new LoggingHandler(LogLevel.INFO));
		bootstrap.childHandler(new ServerChannelInitializer());
		bootstrap.bind(port);
	}
	
	
	public static void addUnRegisterChannel(Channel channel){
		unRegisterChannels.add(channel);
	}
	
	
}
